﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using PServiceBus.Core.Interface;

namespace PServiceBus.Core.Configuration {
    public sealed class ServiceBusConfigSection : ConfigurationSection, IServiceBusConfig {

        [ConfigurationProperty("objectManagerType")]
        public string ObjectManagerType {
            get { return this["objectManagerType"] as string; }
            set { this["objectManagerType"] = value; }
        }

        [ConfigurationProperty("address")]
        public string Address {
            get { return this["address"] as string; }
            set { this["address"] = value; }
        }

        [ConfigurationProperty("messageStoreAddress")]
        public string MessageStoreAddress {
            get { return this["messageStoreAddress"] as string; }
            set { this["messageStoreAddress"] = value; }
        }

        [ConfigurationProperty("deadMessageAddress")]
        public string DeadMessageAddress {
            get { return this["deadMessageAddress"] as string; }
            set { this["deadMessageAddress"] = value; }
        }

        [ConfigurationProperty("resequenceMessageAddress")]
        public string ResequenceMessageAddress {
            get { return this["resequenceMessageAddress"] as string; }
            set { this["resequenceMessageAddress"] = value; }
        }

        [ConfigurationProperty("keyValueStoreAddress")]
        public string KeyValueStoreAddress {
            get { return this["keyValueStoreAddress"] as string; }
            set { this["keyValueStoreAddress"] = value; }
        }

        [ConfigurationProperty("messageRetryCount")]
        public int MessageRetryCount {
            get { return (int)this["messageRetryCount"]; }
            set { this["messageRetryCount"] = value; }
        }

        [ConfigurationProperty("defaultMessageExpiresIn")]
        public TimeSpan DefaultMessageExpiresIn {
            get {
                var expiresIn = this["defaultMessageExpiresIn"] as TimeSpan?;
                if (expiresIn == null) expiresIn = new TimeSpan(30, 0, 0, 0);
                return expiresIn.Value.TotalDays < 1 ? new TimeSpan(30, 0, 0, 0) : expiresIn.Value;
            }
            set { this["defaultMessageExpiresIn"] = value; }
        }

        [ConfigurationProperty("retryMessageInterval")]
        public TimeSpan RetryMessageInterval {
            get {
                var retryMessageInterval = this["retryMessageInterval"] as TimeSpan?;
                if (retryMessageInterval == null) retryMessageInterval = new TimeSpan(0, 0, 0, 0, 1);
                return retryMessageInterval.Value.TotalSeconds < 1 ? new TimeSpan(0, 0, 0, 0, 1) : retryMessageInterval.Value;
            }
            set { this["retryMessageInterval"] = value; }
        }
    }
}
